<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\model\Ad as AdModel;
use app\common\model\ProductCate;
use think\App;
use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use think\exception\ValidateException;
use think\facade\View;
use app\common\model\Product;
use think\response\Json;

class Ad extends Base
{
    //主键，默认为id
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\Ad';

    public function index()
    {
        return $this->setTable();
    }

    public function setTable()
    {
        //构建table
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addRow(['field' => 'title', 'title' => '标题','modify' => 'add|edit','format' => 'input'])
            ->addRow(['field' => 'image', 'title' => '广告图','width' => 250,
                'templet' => '<div><img class="thumbnail" src="{{d.image}}"/></div>',
                'format' => 'img','modify' => 'add|edit'])
            ->addRow(['field' => 'sort', 'title' => '排序', 'width' => 100, 'templet' => '#sort_sort'])
            ->addRow(['field' => 'create_time', 'title' => '创建时间'])
            ->addRow(['field' => 'update_time', 'title' => '更新时间'])
            ->addRow(['field' => 'status', 'title' => '状态', 'templet' => '#status', 'width' => 120, 'unresize' => true])
            ->addRow(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 200]);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->addRightButtons(['edit' => ['width' => 800, 'height' => 600]])
            ->addSwitchTool([['name' => 'status', 'pk' => 'id', 'text' => '开启|关闭']])
            ->setSearch($this->setSearch())
            ->addExtraMap()
            ->fetch();
    }

    public function setSearch()
    {
        return TableFront::getInstance()
            ->setSearch(['title', '输入广告标题', 'input','','','','like']);
    }

    public function add($name = '', $tpl = 'ad/edit')
    {
        View::assign('info', '');
        $this->assginData();
        return parent::add($name, $tpl); // TODO: Change the autogenerated stub
    }

    public function edit($name = '', $id = '', $tpl = 'ad/edit', $info = '')
    {
        $this->assginData();
        $info = AdModel::find($id);
        $info->cate_1 = $info->cate_2 = $info->cate_3 = 0;
        if($info->link_type == 2 && $info->link_id){
            $cate = explode(',', $info->link_id);
            isset($cate[0]) && $info->cate_1 = $cate[0];
            isset($cate[1]) && $info->cate_2 = $cate[1];
            isset($cate[2]) && $info->cate_3 = $cate[2];
        }
        View::assign('info', $info);
        return parent::edit($name, $id, $tpl, $info); // TODO: Change the autogenerated stub
    }

    public function assginData()
    {
        $cates = ProductCate::where('pid', 0)->select();
        $products = Product::where(['is_del'=>0, 'status'=>1])->select();
        View::assign('cates', $cates);
        View::assign('products', $products);
    }

    /*public function add($name = '', $tpl = 'ad/edit')
    {
        View::assign([
            'product' => Product::where(['is_del'=>0, 'status'=>1])->select()
        ]);
        return parent::add($name, $tpl); // TODO: Change the autogenerated stub
    }

    public function edit($name = '', $id = '', $tpl = 'ad/edit', $info = '')
    {
        $info = $this->model::find($id);
        $foreign_name = $info->link_type == 1 ? Product::where('id', $info->foreign_id)->value('title') : '';
        View::assign([
            'foreign_name' => $foreign_name
        ]);
        return parent::edit($name, $id, $tpl, $info); // TODO: Change the autogenerated stub
    }*/

    public function selectProduct(): Json
    {
        $param = $this->request->param();

        if ($this->request->isAjax()) {
            $user = Product::whereLike('title', '%' . $param['val'] . '%')
                ->field('id,title')->where('is_del', 0)->select();
            return json(['code' => 0, 'data' => $user]);
        }
        return json(['code' => -1, 'msg' => '请求错误']);
    }

    public function save($param = [], $model = true): Json
    {
        $param = $this->request->param();
        return parent::save($param, $model); // TODO: Change the autogenerated stub
    }

}
